package org.feng.util;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.feng.enums.BaseEnum;

import java.lang.reflect.Field;

/**
 * TODO
 *
 * @author feng
 */
public class CustomDictEnumConvert implements Converter<BaseEnum<Integer>> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public BaseEnum<Integer> convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String desc = cellData.getStringValue();
        Field field = excelContentProperty.getField();
        Class<?> declaringClass = field.getDeclaringClass();
        return BaseEnum.of(desc, (Class<? extends Enum<?>>) declaringClass);
    }

    @Override
    public CellData<Integer> convertToExcelData(BaseEnum<Integer> value, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value == null) {
            return new CellData<>("");
        }
        return new CellData<>(value.getDesc());
    }
}
